package com.qumitech.visualnovel.service.a8game.impl;

import com.alibaba.fastjson.JSONObject;
import com.qumitech.visualnovel.component.util.HttpInvokeUtil;
import com.qumitech.visualnovel.model.common.BaseRsp;
import com.qumitech.visualnovel.model.dao.console.UserInfo;
import com.qumitech.visualnovel.model.common.UrlConstant;
import com.qumitech.visualnovel.pubdef.dto.a8game.A8GameSpecificLimitConfigQueryAdminDTO;
import com.qumitech.visualnovel.pubdef.dto.a8game.A8GameSpecificLimitConfigRevokeAdminDTO;
import com.qumitech.visualnovel.pubdef.dto.a8game.A8GameSpecificLimitNewAdminDTO;
import com.qumitech.visualnovel.service.a8game.A8GameService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Service;

import java.util.Map;


@Service
public class A8GameServiceImpl implements A8GameService {

    private static final String SPECIFIC_LIMIT_URL= "/admin/a8/game/limit";


    @Override
    public BaseRsp saveSpecificLimit(A8GameSpecificLimitNewAdminDTO dto) {
        dto.setOperator(getActiveUserName());
        String url = UrlConstant.SERVER_URL + SPECIFIC_LIMIT_URL;
        String req = JSONObject.toJSONString(dto);
        String body = HttpInvokeUtil.httpPostJson(url, req, UrlConstant.DefaultEncode, null);
        return JSONObject.parseObject(body, BaseRsp.class);
    }

    @Override
    public BaseRsp getSpecificLimit(A8GameSpecificLimitConfigQueryAdminDTO queryAdminDTO) {
        String url = UrlConstant.SERVER_URL + SPECIFIC_LIMIT_URL;
        JSONObject o = JSONObject.parseObject(JSONObject.toJSONString(queryAdminDTO));
        Map<String, Object> req = JSONObject.toJavaObject(o, Map.class);
        String body = HttpInvokeUtil.httpGet(url, req);
        return JSONObject.parseObject(body, BaseRsp.class);
    }

    @Override
    public BaseRsp rmSpecificLimit(A8GameSpecificLimitConfigRevokeAdminDTO revokeAdminDTO, Long id) {
        revokeAdminDTO.setOperator(getActiveUserName());
        String url = UrlConstant.SERVER_URL + SPECIFIC_LIMIT_URL + "/" + id;
        String req = JSONObject.toJSONString(revokeAdminDTO);
        String body = HttpInvokeUtil.httpPutJson(url, req, UrlConstant.DefaultEncode, null);
        return JSONObject.parseObject(body, BaseRsp.class);

    }

    private String getActiveUserName(){
        Subject subject = SecurityUtils.getSubject();
        UserInfo activeUser = (UserInfo) subject.getPrincipal();
        return activeUser.getUsername();
    }
}
